package edu.zut.test;

import edu.zut.main.pojo.SysUser;
import edu.zut.main.util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class QueryList {

    public static void main(String[] args) throws SQLException {
        queryList();
    }

    /**
     * 查询结果是多条数据时
     */
    public static void queryList() throws SQLException {
        String sql = "select user_id id, user_code code, user_name name, user_password psw, user_state state from sys_user";

        ResultSet rs = DBUtil.query(sql, null);

        List<SysUser> sysUsers = new ArrayList<>();
        //遍历结果集
        while (rs.next()){
            //将结果集中的数据解析出来
            int user_id = rs.getInt("id");
            String user_code = rs.getString("code");
            String user_name = rs.getString("name");
            String user_password = rs.getString("psw");
            int user_state = rs.getInt("state");
            //封装为一个对象
            SysUser sysUser = new SysUser(user_id,user_name,user_password,user_code,user_state);
            sysUsers.add(sysUser);
        }
        System.out.println("sysUsers = " + sysUsers);

    }

}
